<?php
class TcTag extends Tag
{
	public $Tag =array(
		'arclist'=>array('block'=>1,'level'=>3),
		'channel'=>array('block'=>1,'level'=>3),
		'pagelist'=>array('block'=>1,'level'=>3),
		'pagenum'=>array('block'=>0,'level' => 0),
		'pagenext'=>array('block'=>0,'level' => 0)
	);
	public function _pagelist($attr,$content){
		$row=isset($attr['row'])?intval($attr['row']):20;
		$php=<<<str
		<?php
		\$cid=\$_GET['cid'];
		\$data=Data::channelList(F("category"),\$cid,'','cid');
		\$tmp=array(\$cid);
		foreach(\$data as \$d){
			\$tmp[]=\$d['cid'];
		}
		\$where='catid in('.implode(',',\$tmp).')';
		\$db=K('Article');
		\$row=$row;
		\$page =new page(\$db->where(\$where)->count(),\$row);
		\$result=\$db->where(\$where)->limit(\$page->limit())->all();
		if(\$result):
		foreach(\$result as \$field):
			\$field['url'] = U('show',array('id'=>\$field['id']));
			\$field['caturl']=U('channel',array('cid'=>\$field['cid']));
		;?>
str;
		$php.=$content;
		$php.="<?php endforeach;endif;?>";
		return $php;
	}
	public function _pagenum($attr,$content){
		$style=isset($attr['style'])?intval($attr['style']):2;
		return '<?php echo $page->show('.$style.');?>';
	}
	public function _channel($attr,$content){
		$type=isset($attr['type'])?$attr['type']:'self';
		$cid=isset($attr['cid'])?intval($attr['cid']):0;
		$php=<<<str
		<?php
		\$type='$type';
		\$cid = $cid?cid:Q('cid',null,'intval');
		\$db=M('category');
		switch(\$type){
			case 'self':
			if(\$cid){
				\$pid=\$db->where("cid=\$cid")->getField('pid');
				\$result=\$db->where("pid=\$pid")->all();
			}
			break;
			case 'son':
			if(\$cid){
				\$result=\$db->where("pid=\$cid")->all();
			}
			break;
			case 'top':
			\$result=\$db->where('pid=0')->all();
			break;
		}
		if(\$result):
		foreach(\$result as \$field):
			\$field['url']=U('channel',array('cid'=>\$field['cid']));
		;?>
str;
		$php.=$content;
		$php.="<?php endforeach;endif;?>";
		return $php;
	}
	public function _arclist($attr,$content){
		//显示条数
		$row = isset($attr['row'])?$attr['row']:10;
		$row = empty($row)?10:$row;
		//显示类型 hot 热门文章   new  最新文章
		$type = isset($attr['type'])?$attr['type']:'new';
		//是否只获取有缩略图的文章，1获取只有图片的 0有没有图都行
		$pic = intval(isset($attr['pic'])?$attr['pic']:0);
		//获取指定栏目
		$cid = isset($attr['cid'])?$attr['cid']:'';
		//获取指定文章
		$id = intval(isset($attr['id'])?$attr['id']:0);
		$php=<<<str
	<?php
		\$cid ='$cid';
		\$cid=empty(\$cid)?\$_GET['cid'] : (int)\$cid;
		\$db=M("article");
		\$type='$type';
		if(\$cid){
			\$data=Data::channelList(F("category"),\$cid,'','cid');
			\$tmp=array(\$cid);
			foreach(\$data as \$d){
				\$tmp[]=\$d['cid'];
			}
			\$where='catid in('.implode(',',\$tmp).')';
			\$db->where(\$where);
		}
		if(\$id){
			\$db->where("id=\$id");
		}
		if($pic){
			\$db->where("thumb<>''");
		}
		switch(\$type){
			case 'hot':
				\$db->order('click desc');
			break;
			default:
				\$db->order('id desc');
		}
		\$result= \$db->limit($row)->all();
		if(\$result):
		foreach(\$result as \$field):
			\$field['url'] = U('show',array('id'=>\$field['id']));
		?>
str;
		$php.=$content;
		$php.="<?php endforeach;endif;?>";
		return $php;
	}
	public function _pagenext($attr,$content){
		$pre = isset($attr['pre'])?$attr['pre']:'上一篇：';
		$next = isset($attr['next'])?$attr['next']:'下一篇：';
		$php=<<<str
		<?php
		\$db =M("article");
		\$id =Q("id",null,'intval');
		\$cid =\$tc['catid'];
		\$result=\$db->where("id<\$id AND catid=\$cid")->order("id DESC")->find();
		if(\$result){
			echo '$pre <a href="'.U('show',array('id'=>\$result['id'])).'">'.\$result['title'].'</a>';
		}else{
			echo '$pre 没有了……';
		}
		\$result=\$db->where("id>\$id AND catid=\$cid")->order("id ASC")->find();
		if(\$result){
			echo '&nbsp;&nbsp;&nbsp;&nbsp; $next <a href="'.U('show',array('id'=>\$result['id'])).'">'.\$result['title'].'</a>';
		}else{
			echo '&nbsp;&nbsp;&nbsp;&nbsp; $next 没有了……';
		}
		
		?>
str;
		return $php;
	}
}